package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.entity.DTO.CirculatingCol;
import com.museum.entity.message.Circulating;
import com.museum.entity.message.ExcavationInformation;
import com.museum.mapper.CirculatingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CirculatingServiceImpl extends ServiceImpl<CirculatingMapper, Circulating> {
    @Autowired
    private FeignAPIController feignAPIController;
    @Autowired
    private com.museum.mapper.CirculatingMapper circulatingMapper;


    /**
     * @param
     * @param request
     * @Desciption 添加流传经历
     * @return
     */

    public int addCirculating(Circulating circulating, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();

        return circulatingMapper.insert(circulating);

    }

    /**
     * @Description 删除流传经历
     *
     */
    public int deleteCirculatingById(Integer id){
        return  circulatingMapper.deleteById(id);
    }




    /**
     * @param
     * @Desciption 条件查询流传经历
     * @return
     */
    public JSONObject getAllCirculatingByPage(long current, long limit, CirculatingCol circulatingCol){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<Circulating> circulatingList = new ArrayList<>();
        Integer id =null;
        String startTime = null;
        String endTime = null;
        String collectOrganization = null;
        String collector = null;
        String note = null;
        if(circulatingCol!=null){
            id = circulatingCol.getId();
            startTime = circulatingCol.getStartTime();
            endTime = circulatingCol.getEndTime();
            collectOrganization = circulatingCol.getCollectOrganization();
            collector = circulatingCol.getCollector();
            note = circulatingCol.getCollector();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(startTime)) {
            map.put("startTime",startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            map.put("endTime",endTime);
        }
        if (!StringUtils.isEmpty(collectOrganization)) {
            map.put("collectOrganization",collectOrganization);
        }
        if (!StringUtils.isEmpty(collector)) {
            map.put("collector",collector);
        }

        if (!StringUtils.isEmpty(note)) {
            map.put("note",note);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<Circulating> list = circulatingMapper.selectCirculationByPage(map);
        for (Circulating circulating : list) {
            circulatingList.add(circulating);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",circulatingList.size());
        jsonObject.put("circulatingList", circulatingList);
        return jsonObject;
    }


}
